  <template>
  <div>
    <el-dialog title="警告" :visible.sync="dialogFormVisible">
      <el-form>
        <el-form-item label="为确保是管理员输入，请输入管理员手机号码">
          <el-input v-model="password" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button @click="updateSql" type="primary">确 定</el-button>
      </div>
    </el-dialog>
    <el-table :data="tableData" stripe border style="width: 100%">
      <el-table-column type="index" width="50"> </el-table-column>
      <el-table-column prop="_id" label="_id" width="180"> </el-table-column>
      <el-table-column prop="account" label="用户账号" width="180">
      </el-table-column>
      <el-table-column prop="selectLib" label="科目" width="110">
      </el-table-column>
      <el-table-column prop="question" label="题目"> </el-table-column>
      <el-table-column prop="time" label="提交时间戳"> </el-table-column>
      <el-table-column prop="formatTime" label="提交时间"> </el-table-column>
      <el-table-column fixed="right" label="操作" width="100">
        <template slot-scope="scope">
          <el-button @click="handleClick(scope.row)" type="text" size="small"
            >编辑</el-button
          >
          <el-button type="text" size="small" @click="cancelClick(scope.row)"
            >撤销</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      background
      @current-change="pageChangeHandler"
      layout="prev, pager, next"
      :total="1000"
    >
    </el-pagination>
  </div>
</template>

<script>
import feedBackApi from "@/api/tiku/feedback";
import timeApi from "@/utils/time";
export default {
  data() {
    return {
      tableData: [],
      dialogFormVisible: false,
      password: 0,
      cancelItem: [], //用户点击撤销时需要提交到数据器中的数据
      currentPage: 1,
    };
  },
  created: function () {
    this.getFeedBack();
  },
  methods: {
    pageChangeHandler: async function (e) {
      console.log(e);
      this.currentPage = e;
      this.getFeedBack();
    },
    getFeedBack: async function () {
      let res = await feedBackApi.getFeedBack(this.currentPage);
      let data = res.data.data;
      console.log(data);
      this.tableData = [];
      for (let i = 0; i < data.length; i++) {
        const element = data[i];
        const obj = JSON.parse(element);
        let { _id, account, question, selectLib, time } = obj;
        //TODO 这里后期需要处理显示的问题
        let formatTime = await timeApi.timeFormat(time);
        this.tableData.push({
          account,
          question,
          selectLib,
          time,
          _id,
          formatTime,
        });
      }
    },
    /**
     *
     */
    updateSql: async function () {
      console.log(this.password, this.cancelItem);
      let { question, selectLib, _id, account, time } = this.cancelItem;
      if (this.password != 18527307932) {
        this.$message.error("输入密码错误");
        return;
      }
      console.log(account, time);
      let remove = await feedBackApi.remove(account, time);
      console.log(remove.data);
      if ((remove.data = 20000)) {
        this.$message({
          message: "删除成功",
          type: "success",
        });
        this.getFeedBack();
        //刷新页面
        window.location.reload();
      }
    },

    handleClick: async function (e) {
      let { question, selectLib, _id, account, time } = e;
      console.log(question, selectLib, _id, account, time);
      const encodedQuestion = encodeURIComponent(question);
      const encodedSelectLib = encodeURIComponent(selectLib);
      const encodedId = encodeURIComponent(_id);
      const encodedAccount = encodeURIComponent(account);
      const encodedTime = encodeURIComponent(time);
      //对路径进行编码
      this.$router.push(
        `/feedback/feedback1/list/${encodedQuestion}/${encodedSelectLib}/${encodedId}/${encodedAccount}/${encodedTime}`
      );
    },
    cancelClick: async function (e) {
      let { question, selectLib, _id, account, time } = e;
      this.cancelItem = { question, selectLib, _id, account, time };
      this.dialogFormVisible = true;
    },
  },
};
</script>

<style>
</style>